<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.DriverManager" %><%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2018/4/2
  Time: 19:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>userList</title>

</head>
<body>
<h2>用户</h2>

<%
    //判断用户是否登录
   if(session.getAttribute("isLogin")==null) {
       //没有登录的话就跳转到login.jsp
       response.sendRedirect("login.jsp");
   }
%>

<style type="text/css">
    table {
        border-collapse: collapse;

    }

    td {
        border: 1px solid #000;
        padding: 10px;
    }
</style>

<table>
    <tr>
        <th>编号</th>
        <th>用户名</th>
        <th>密码</th>
        <%--<th>操作1</th>--%>
        <%--<th>操作2</th>--%>
    </tr>

    <%



        class User {
            int id;
            String username;
            String password;
        }

        List<User> userList = new ArrayList<User>();

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        String sql = "select*from t_users";
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sctu_javaweb_2018?serverTimezone=UTC",
                "root",
                "123456"
        );
        statement = connection.createStatement();
        resultSet = statement.executeQuery(sql);

//        resultSet->List
        while (resultSet.next()) {
            int id = resultSet.getInt(1);
            String username = resultSet.getString(2);
            String password = resultSet.getString(3);

            //面向对象三个特征：封装、继承、多态
            User user = new User();
            user.id = id;
            user.username = username;
            user.password = password;

            userList.add(user);
        }

        connection.close();
        statement.close();
        resultSet.close();
        //从数据库中读取数据，并且放在userList中   3+1
//        User chen=new User();
//        chen.id=1;
//        chen.username="chen";
//        chen.password="123345";
//        userList.add(chen);

        //遍历
        for (User user : userList) {

            //221  替换原则
            out.write("<tr><td>" + user.id + "</td><td>"
                    + user.username + "</td><td>"
                    + user.password + "</td></tr>");
        }
    %>


</table>


<%--jsp指令   包含静态信息--%>
<%--<%@ include file="footer.jsp"%>--%>

<%--jsp动作  ：  有jsp前缀    动态信息--%>
<jsp:include page="footer.jsp">
    <jsp:param name="year" value="2018"/>
</jsp:include>
</body>
</html>
